<template>
  <div>
    <div class="formbox">
      <h1>Zyqq个人博客</h1>
      <el-form
        ref="Formlogin"
        class="elform"
        :model="LoginForm"
        :rules="Loginrules"
      >
        <el-form-item
          label="用户名"
          prop="username"
        >
          <el-input
            placeholder="请输入用户名"
            class="Logininput"
            v-model="LoginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input
            placeholder="请输入密码"
            class="Logininput"
            type="password"
            v-model="LoginForm.password"
          ></el-input>
        </el-form-item>
        <div class="buttonbox">
          <el-button
            type="primary"
            @click="fnLogin"
          >
            登录
          </el-button>
          <el-button @click="$router.push('/Login')">
            注册
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      LoginForm: {
        username: "admin",
        password: "123123",
      },
      Loginrules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    Alertsuccess(msg, url) {
      this.$message({
        message: msg,
        type: "success",
        duration: "1000",
        onClose: () => (url ? this.$router.push(url) : ""),
      });
    },
    fnLogin() {
      this.$refs.Formlogin.validate((valid) => {
        if (valid) {
          this.axios
            .post("/api/Login", {}, { params: this.LoginForm })
            .then((res) => {
              if (res) {
                // console.log(res);
                localStorage.setItem("token", res.Data);
                this.Alertsuccess(res.Message, "/Home");
              }
            });
        }
      });
    },
  },
};
</script>
<style scoped>
.formbox {
  width: 500px;
  height: 400px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid #409eff;
  display: flex;
  flex-direction: column;
  border-radius: 10%;
  text-align: center;
}
h1 {
  padding-top: 40px;
}
.elform {
  padding-top: 40px;
  padding-left: 40px;
  text-align: right;
}
.Logininput {
  width: 300px;
  padding-right: 80px;
}
.buttonbox {
  padding-top: 40px;
  display: flex;
  justify-content: space-around;
}
</style>